<?php
/**
 * Created by PhpStorm.
 * User: dingjinlai
 * Date: 2019/9/3
 * Time: 下午2:22
 */

defined('BASEPATH') OR exit('No direct script access allowed');

class Ueeshop_api
{
    public $web_name = '';

    private $secret = '0c0db6752fb3a5bdec01';

    private $key = 'UPAK193';

    private $url = '';

    public $web_access = '';

    public $ab_name = '';

    public $web_id = '';

    public function __construct($params = array())
    {

        $this->initialize($params);
        log_message('info', 'Pagination Class Initialized');
    }

    public function initialize($params = array())
    {
        $this->web_name = $params['web_name'];
        $this->web_access = $params['web_access'];
        $this->ab_name = $params['ab_name'];
        $this->url = $params['web_access'];
        $this->web_id = $params['web_id'];
        return $this;
    }

    public function get_cate()
    {
        $time = time();

        $_data = array(
            'ApiName' => 'openapi',
            'Number' => $this->key,
            'Action' => 'sync_get_products_category',
            'timestamp' => $time,
            'Language' => 'en',
            'Count' => 1000
        );

        $_data['sign'] = $this->sign($_data);

        $rm = $this->uee_curl($_data);

        return $rm;
    }

    public function get_warehouse()
    {
        $time = time();

        $_data = array(
            'ApiName' => 'openapi',
            'Number' => $this->key,
            'Action' => 'sync_get_warehouse',
            'timestamp' => $time,
            'Language' => 'en',
            'Count' => 1000
        );

        $_data['sign'] = $this->sign($_data);

        $rm = $this->uee_curl($_data);

        return $rm;
    }

    public function get_attr()
    {
        $time = time();

        $_data = array(
            'ApiName' => 'openapi',
            'Number' => $this->key,
            'Action' => 'sync_get_attribute_category',
            'timestamp' => $time,
            'Language' => 'en'
        );

        $_data['sign'] = $this->sign($_data);

        $rm = $this->uee_curl($_data);

        return $rm;
    }

    public function get_attr_val($attr_id)
    {
        $time = time();

        $_data = array(
            'ApiName' => 'openapi',
            'Number' => $this->key,
            'Action' => 'sync_get_attribute_value',
            'timestamp' => $time,
            'AttrId' => $attr_id,
            'Language' => 'en'
        );

        $_data['sign'] = $this->sign($_data);

        $rm = $this->uee_curl($_data);

        return $rm;
    }


    public function get_goods($goods_sn)
    {
        $time = time();

        $_data = array(
            'ApiName' => 'openapi',
            'Number' => $this->key,
            'Action' => 'sync_get_products',
            'timestamp' => $time,
            'SKU' => $goods_sn,
            'Language' => 'en'
        );

        $_data['sign'] = $this->sign($_data);

        $rm = $this->uee_curl($_data);

        return $rm;
    }

    public function add_goods($data = array())
    {
        $time = time();

        $_img = array();
        foreach ($data['images'] as $row) {
            $_img[] = $row['src'];
        }

        $_data = array(
            'ApiName' => 'openapi',
            'Number' => $this->key,
            'Action' => 'sync_product',
            'timestamp' => $time,
            'Currency' => 'USD',
            'Name' => 'en:' . $data['goods_name'],
            'CateId' => $data['cate_id'],
            'No' => $data['goods_id'],
            'SKU' => $data['goods_sn'],
            'PurchasePrice' => $data['in_price'],
            'Price_0' => $data['market_price'],
            'Price_1' => $data['shop_price'],
            'PicPath' => implode(';', $_img),
            'Weight' => floatval($data['goods_weight'] > 50 ? ($data['goods_weight'] / 1000) : 0.06),
            'MOQ' => 1,
            'IsCombination' => 1,
            'AttrId' => (int)$data['AttrId'],
            'Attr' => json_encode(array(1 => $data['variants']['Attr'])),
            'ExtAttr' => json_encode($data['variants']['ExtAttr']),
            'Stock' => 999,
            //'AttrColorImage' =>
            'IsNew' => $data['IsNew'],
            'Description_en' => $data['body_html']
        );

        $_data['sign'] = $this->sign($_data);

        $rm = $this->uee_curl($_data);

        return $rm;
    }

    private function sign($signData){	//生成签名
        $str='';
        $data=array();
        foreach($signData as $k=>$val){
            $k=trim($k);
            $data[$k]=trim($val);
        }
        ksort($data);
        foreach($data as $k=>$v){
            if($k=='sign' || $v===''){continue;}
            $str.="$k=$v&";
        }
        return md5($str.'key='.$this->secret);
    }

    private function uee_curl($post_data = array(), $method = 'POST')
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->url.'/gateway/');
        //curl_setopt($ch, CURLOPT_HEADER, 0);

        if ($post_data || $method == 'POST') {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        }

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);

        $content = curl_exec($ch);

        curl_close($ch);


        print_r($content);

        return json_decode($content, true);
    }

}
